package demo.sql;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class D03_wmtest {


    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);


        String sourceSql = "CREATE TABLE datagen (" +
                "    f_sequence INT," +
                "    f_random INT," +
                "    f_random_str STRING," +
                "    ts AS localtimestamp," +
                "    WATERMARK FOR ts AS ts" +
                "  ) WITH (" +
                "    'connector' = 'datagen'," +
                "    'rows-per-second'='1'," +
                "    'fields.f_sequence.kind'='sequence'," +
                "    'fields.f_sequence.start'='1'," +
                "    'fields.f_sequence.end'='10000'," +
                "    'fields.f_random.min'='1'," +
                "    'fields.f_random.max'='500'," +
                "    'fields.f_random_str.length'='10'" +
                "  )";

        String sinkSql =
                "  CREATE TABLE print_table (" +
                "    f_sequence INT," +
                "    f_random INT," +
                "    f_random_str STRING," +
                "    ts timestamp(3)" +
                "    ) WITH (" +
                "    'connector' = 'print'" +
                "  )";

        String execSql = "INSERT INTO print_table select f_sequence,f_random,f_random_str,ts from datagen where ts < CURRENT_WATERMARK(ts)";

        tableEnvironment.executeSql(sourceSql);
        tableEnvironment.executeSql(sinkSql);
        tableEnvironment.executeSql(execSql);


    }

}
